﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Induction.FinalTest
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GeneralLinqToSQLDataContext dc = new GeneralLinqToSQLDataContext();

            var defaultCurrency =
                from c in dc.Countries
                select new { c.CountryID, c.Currency };
            var country =
                from c in dc.Countries
                select new { c.CountryID, c.CountryName };
            foreach (var item in defaultCurrency)
            {
                ListOfCurrency.Items.Add(new ListItem(item.Currency.ToString(),item.CountryID.ToString()));
            }

            foreach (var item in country)
            {
                ListOfCountry.Items.Add(new ListItem(item.CountryName.ToString(), item.CountryID.ToString()));
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            
            if (Page.IsValid)
            {
                GeneralLinqToSQLDataContext dc = new GeneralLinqToSQLDataContext();
                Customer c = new Customer();
                c.CustomerNumber = CustomerNumberText.Text;
                c.CustomerName = CustomerNameText.Text;
                c.Status = ListOfStatus.SelectedValue;
                c.DefaultCurrency = Int32.Parse(ListOfCurrency.SelectedValue);
                c.Address1 = Address1Text.Text;
                c.Address2 = Address2Text.Text;
                c.Address3 = Address3Text.Text;
                c.City = CityText.Text;
                c.Country = Int32.Parse(ListOfCountry.SelectedValue);
                c.Email = EmailText.Text;
                c.Website = WebsiteText.Text;
                c.PhoneNumber = PhoneText.Text;
                c.FaxNumber = FaxText.Text;
                c.PostCode = PostCodeText.Text;
                c.State = StateText.Text;

                dc.Customers.InsertOnSubmit(c);
                dc.SubmitChanges();

                Response.Redirect("Report.aspx");
            }
        }
    }
}